package com.assembly.system.service.impl;

import com.assembly.entities.Role;
import com.assembly.entities.RoleUser;
import com.assembly.system.mapper.RoleMapper;
import com.assembly.system.req.RoleREQ;
import com.assembly.system.service.IRoleService;
import com.assembly.system.service.IRoleUserService;
import com.assembly.util.base.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.WeakHashMap;

/**
 * <p>
 * 平台角色信息表 服务实现类
 * </p>
 *
 * @author 丽人平台-www.assembly.com
 * @since 2021-03-29
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    /***
     * 角色新增
     * @param role 角色对象
     */
    @Override
    public void saveRole(Role role) {
        baseMapper.insert(role);
    }

    /***
     * 修改角色
     * @param role
     */
    @Override
    public void updateRole(Role role) {
        role.setUpdateDate(new Date());
        baseMapper.updateById(role);
    }

    /***
     * 角色列表查询
     * @param roleREQ
     * @return
     */
    @Override
    public IPage<Role> search(RoleREQ roleREQ) {
        QueryWrapper<Role> wrapper =new QueryWrapper<>();
        wrapper.eq("school_id",roleREQ.getId());
        if (StringUtils.isNotEmpty(roleREQ.getRoleName())){
            wrapper.eq("role_name",roleREQ.getRoleName());
        }
        return baseMapper.selectPage(roleREQ.getPage(),wrapper);
    }


    /***
     * 角色 下拉查询
     * @param mechanismId 机构（ 学校机构ID ）
     * @return
     */
    @Override
    public Result queryDropDownByMechanismId(String mechanismId) {
        if (StringUtils.isEmpty(mechanismId)){
            return  Result.error("机构ID并不能为空！");
        }
        QueryWrapper<Role> wrapper =new QueryWrapper<>();
        wrapper.eq("school_id",mechanismId);
        return  Result.ok(baseMapper.selectList(wrapper));
    }

    @Override
    @Transactional
    public void deleteRole(List<String> roleIds) {
        baseMapper.deleteRoleUserBatch(roleIds);
        baseMapper.deleteBatchIds(roleIds);
    }

}
